package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.PageUtil;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongshop.service.ContentService;
import org.apache.zookeeper.data.Stat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController {
    @Reference
    private ContentCategoryService contentCategoryService;
    @Reference
    private ContentService contentService;

    @RequestMapping("toContentCategory")
    //跳转广告类型管理
    public String toContentCategory(){
        return "admin/content_category";
    }

    @RequestMapping("toContent")
    //跳转广告类型管理
    public String toContent(Model model){
        //查询广告类型
        List<TbContentCategory> list = contentCategoryService.findall("");
        model.addAttribute("list",list);
        return "admin/content";
    }


    //查询所有
    @RequestMapping("fidall")
    @ResponseBody
    public StatusCode fidall(String name){
        List<TbContentCategory> list = contentCategoryService.findall(name);
        return StatusCode.build(ConstantEnum.SUCCESS).setT(list);
    }

    //增加
    @RequestMapping("insertcategory")
    @ResponseBody
    public StatusCode insertcategory(TbContentCategory tbContentCategory){
        Boolean b = contentCategoryService.insertcategory(tbContentCategory);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.UPDATE_ERROR);
    }

    //删除
    @RequestMapping("deletecategory")
    @ResponseBody
    public StatusCode deletecategory(Long[] ids){
        Boolean b = contentCategoryService.deletecategory(ids);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.UPDATE_ERROR);
    }

    //回显
    @RequestMapping("selectById")
    @ResponseBody
    public TbContentCategory selectById(Long id){
        TbContentCategory tbContentCategory = contentCategoryService.selectById(id);
        return tbContentCategory;
    }

    //修改
    @RequestMapping("updateCategory")
    @ResponseBody
    public StatusCode updateCategory(TbContentCategory tbContentCategory){
        if (tbContentCategory == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = contentCategoryService.updateCategory(tbContentCategory);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.UPDATE_ERROR);
    }

    //查询广告管理所有
    @RequestMapping("findallContent")
    @ResponseBody
    public StatusCode findallContent(){
        List<TbContent> list = contentService.findallContent();
        return StatusCode.build(ConstantEnum.SUCCESS).setT(list);
    }

    //增加
    @RequestMapping("insertContent")
    @ResponseBody
    public StatusCode insertContent(TbContent tbContent){
        Boolean b = contentService.insertContent(tbContent);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.UPDATE_ERROR);
    }

    //回显
    @RequestMapping("selectContentById")
    @ResponseBody
    public StatusCode selectContentById(Long id){
        if (id == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        TbContent tbContent = contentService.selectContentById(id);
        return StatusCode.build(ConstantEnum.SUCCESS).setT(tbContent);
    }

    //修改
    @RequestMapping("updateContent")
    @ResponseBody
    public StatusCode updateContent(TbContent tbContent){
        Boolean b = contentService.updateContent(tbContent);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.UPDATE_ERROR);
    }

    //删除
    @RequestMapping("deleteContent")
    @ResponseBody
    public StatusCode deleteContent(Long[] ids){
        Boolean b = contentService.deleteContent(ids);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.UPDATE_ERROR);
    }

}
